from sqlalchemy import Column, DateTime, BigInteger, String, CHAR, func
from base import Base
from sqlalchemy.orm import relationship

class SysUser(Base):
    __tablename__ = 'sys_users'

    id = Column(BigInteger, primary_key=True, autoincrement=True, comment='用户ID')
    dept_id = Column(BigInteger, nullable=True, comment='部门ID')
    user_name = Column(String(30), nullable=False, comment='用户账号')
    nick_name = Column(String(30), nullable=False, comment='用户昵称')
    user_type = Column(String(2), default='00', comment='用户类型（00系统用户）')
    email = Column(String(50), default='', comment='用户邮箱')
    phone_number = Column(String(11), default='', comment='手机号码')
    sex = Column(CHAR(1), default='0', comment='用户性别（0男 1女 2未知）')
    avatar = Column(String(100), default='', comment='头像地址')
    password = Column(String(100), default='', comment='密码')
    status = Column(CHAR(1), default='0', comment='帐号状态（0正常 1停用）')
    del_flag = Column(CHAR(1), default='0', comment='删除标志（0代表存在 2代表删除）')
    login_ip = Column(String(128), default='', comment='最后登录IP')
    login_date = Column(DateTime, nullable=True, comment='最后登录时间')
    create_by = Column(String(64), default='', comment='创建者')
    created_at = Column(DateTime, default=func.now(), comment='创建时间')
    update_by = Column(String(64), default='', comment='更新者')
    updated_at = Column(DateTime, onupdate=func.now(), comment='更新时间')
    deleted_at = Column(DateTime, nullable=True, comment='删除时间')
    remark = Column(String(500), nullable=True, comment='备注')
    posts = relationship('SysPost', secondary='sys_user_posts', back_populates='users')
    def __repr__(self):
        return f"<SysUser(user_id={self.id}, user_name={self.user_name})>"
